package com.awphone.data;

import com.awphone.data.constant.AbstractObjectConst;
import com.awphone.data.constant.EObjectType;
import com.awphone.utils.json.IJsonObject;
import com.awphone.utils.json.JsonUtils;
import com.google.gson.JsonObject;

import javax.persistence.*;
import java.io.Serializable;

/**
 * @author a_polyakov
 */
@MappedSuperclass
public abstract class AbstractObject implements IJsonObject, Serializable {
    /**
     * тип объекта
     */
    private EObjectType objectType;
    /**
     * идентификатор
     */
    private Long id;

    protected AbstractObject() {
    }

    protected AbstractObject(Long id) {
        this.id = id;
    }

    @Column(name = AbstractObjectConst.S_OBJECT_TYPE, nullable = false, updatable = false, length = 25)
    @Enumerated(EnumType.STRING)
    public final EObjectType getObjectType() {
        return objectType;
    }

    public final void setObjectType(EObjectType objectType) {
        this.objectType = objectType;
    }

    @Id
    @Column(name = AbstractObjectConst.S_ID)
    @GeneratedValue(strategy = GenerationType.AUTO)
    public final Long getId() {
        return id;
    }

    public final void setId(Long id) {
        this.id = id;
    }

    public void prePrePersist() {
    }

    @PrePersist
    public final void prePersist() {
        prePrePersist();
    }

    public void prePreUpdate() {
    }

    @PreUpdate
    public final void preUpdate() {
        prePreUpdate();
    }

// ------------------------------- JSON ---------------------------------------

    public AbstractObject(final JsonObject jsonObject) {
        this.objectType = JsonUtils.getEnum(jsonObject, AbstractObjectConst.S_OBJECT_TYPE, EObjectType.class);
        this.id = JsonUtils.getLong(jsonObject, AbstractObjectConst.S_ID);
    }

    @Override
    public JsonObject toJsonObject() {
        final JsonObject jsonObject = new JsonObject();
        JsonUtils.set(jsonObject, AbstractObjectConst.S_OBJECT_TYPE, objectType);
        JsonUtils.set(jsonObject, AbstractObjectConst.S_ID, id);
        return jsonObject;
    }

// ------------------------------ String --------------------------------------

    @Override
    public final String toString() {
        return toJsonObject().toString();
    }

    // ------------------------------ Compare -------------------------------------
    @Override
    public boolean equals(Object o) {
        boolean equals = false;
        if (this == o) {
            equals = true;
        } else if (o instanceof AbstractObject) {
            AbstractObject b = (AbstractObject) o;
            if (id != null ? id.equals(b.id) : b.id == null
                    && objectType == b.objectType)
                equals = true;
        }
        return equals;
    }

    @Override
    public int hashCode() {
        int result = objectType != null ? objectType.hashCode() : 0;
        result = 31 * result + (id != null ? id.hashCode() : 0);
        return result;
    }
}
